<template>
  <div class="app-container">
    <SearchForm
      v-show="showSearch"
      v-model="queryParams"
      :options="options.formOps"
      @onQuery="getList"
    >
    </SearchForm>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['safetyRulesRegulations:create']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <ImportExcel
          type="primary"
          full-path="/safetyRulesRegulations/importData"
          v-hasPermi="['safetyRulesRegulations:importData']"
          plain
          icon="Plus"
          @success="getList"
        ></ImportExcel>
      </el-col>
      <el-col :span="1.5">
        <ExportExcel
          type="primary"
          full-path="/safetyRulesRegulations/importTemplate"
          v-hasPermi="['safetyRulesRegulations:importTemplate']"
          plain
        ></ExportExcel>
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <Table
      ref="table"
      :auto-featch="true"
      :fetch-data="fetchData"
      :options="options.table"
      actionWidth="200"
    >
      <template #default>
        <el-table-column label="是否启用" align="center">
          <template #default="{ row }">
            {{ row.isEnable == 0 ? "是" : "否" }}
          </template>
        </el-table-column>
        <el-table-column label="启用日期" align="center" prop="enableDate">
        </el-table-column>
      </template>
      <template #actionButton="{ row }">
        <el-button type="text" icon="Edit" @click="handleFiles(row)"
          >附件</el-button
        >
        <el-button
          type="text"
          icon="Edit"
          @click="handleEdit(row)"
          v-hasPermi="['safetyRulesRegulations:update']"
          >修改</el-button
        >

        <el-button
          type="text"
          icon="Delete"
          style="color: red"
          @click="handleDel(row)"
          v-hasPermi="['safetyRulesRegulations:delete']"
          >删除</el-button
        >
      </template></Table
    >
    <DialogVform
      :formJson="formJson"
      ref="dialogVform"
      @onSubmit="onSubmit"
      :slots="['publishDeptName', 'applyDeptName']"
    >
      <template #publishDeptName>
        <el-form-item label="发布部门" prop="planDeptId" label-position="right">
          <TreeSelect
            v-model="form.publishDeptId"
            :options="deptTree"
            @change="changepublish"
            style="width: 100%"
          >
          </TreeSelect>
        </el-form-item>
      </template>
      <template #applyDeptName>
        <el-form-item label="适用部门" prop="planDeptId" label-position="right">
          <TreeSelect
            v-model="form.applyDeptId"
            :options="deptTree"
            @change="changeapply"
            style="width: 100%"
          >
          </TreeSelect>
        </el-form-item>
      </template>
    </DialogVform>
    <FileDialog
      title="文件"
      ref="fileDialog"
      :limit="10000"
      @onSubmit="onFileSubmit"
    ></FileDialog>
  </div>
</template>

<script setup>
import Table from "@/components/Table/index.vue";
import SearchForm from "@/components/searchForm/index";
import DialogVform from "@/components/dialogVform/index";
import { apis as APIS } from "@/api/aqsc/aqgz.js";
import formJson from "./components/options.json";
import { useAsyncData, useFormJson } from "@/views/hooks/useAsyncData.js";
import { deptTreeSelect } from "@/api/system/user";
import FileDialog from "@/components/filesDialog";
import ImportExcel from "@/components/ImportExcel/index.vue";
import ExportExcel from "@/components/ExportExcel/index.vue";
const { proxy } = getCurrentInstance();
const ops = useFormJson(formJson, {
  sort: [],
  sortable: [],
});
const options = reactive({
  table: ops,
  formOps: ops.filter((i) =>
    ["docType", "docNumber", "docName"].includes(i.prop)
  ),
});
const showSearch = ref(true);
const dialogVform = ref();
const table = ref();
const plugEdit = (row) => {
  form.value.publishDeptName = row.publishDeptName;
  form.value.publishDeptId = row.publishDeptId;
  form.value.applyDeptName = row.applyDeptName;
  form.value.applyDeptId = row.applyDeptId;
};
const {
  onSubmit,
  fetchData,
  handleAdd,
  handleEdit,
  getList,
  handleDel,
  queryParams,
  form,
} = useAsyncData({ APIS, formJson, dialogVform, table, proxy, plugEdit });
const deptTree = ref([]);
onMounted(() => {
  deptTreeSelect().then((response) => {
    deptTree.value = response.data;
  });
});
const changepublish = (val, label) => {
  form.value.publishDeptName = label;
  form.value.publishDeptId = val;
};
const changeapply = (val, label) => {
  form.value.applyDeptName = label;
  form.value.applyDeptId = val;
};
// 附件
const fileDialog = ref();
const selectRow = ref({});
const handleFiles = (row) => {
  selectRow.value = row;
  fileDialog.value.open(row.rulesRegulationsFileList);
};
const onFileSubmit = (files) => {
  APIS.update({
    ...selectRow.value,
    fileId: files.map((item) => item.id).join(","),
  });
};
</script>

<style lang="scss" scoped></style>
